package com.sonix.base;

import android.Manifest;
import android.app.AlertDialog;
import android.app.Dialog;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.sonix.app.App;
import com.sonix.oidbluetooth.R;
import com.sonix.util.LogUtils;
import com.sonix.util.statusbar.ImmersiveUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.concurrent.CopyOnWriteArrayList;

public abstract class BaseActivity extends AppCompatActivity {

    protected final String TAG = this.getClass().getName();

    //权限
    protected static final int PERMISSION_REQUEST1 = 10;
    protected static final int PERMISSION_REQUEST2 = 20;

    //蓝牙
    protected static final int REQUEST_BLUETOOTH_ENABLE = 30;
    protected static final int REQUEST_SEARCH_DEVICE = 40;

    protected Context mContext;

    private Toast mToast;
    private Dialog mDialog;

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        LogUtils.i(TAG,"onNewIntent");
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());

        mContext = this;
        initView();
        initData();

        //setStatusBar();

        //判断是否需要注册EventBus
        if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
            EventBus.getDefault().register(this);
        }

        setKeyBoardHerlper();

        App.getInstance().activityCreate(this);
        LogUtils.i(TAG,"onCreate");
    }

    @Override
    protected void onStart() {
        super.onStart();
        LogUtils.i(TAG,"onStart");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        LogUtils.i(TAG,"onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        App.getInstance().activityResume(this);
        LogUtils.i(TAG,"onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        LogUtils.i(TAG,"onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        LogUtils.i(TAG,"onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
            EventBus.getDefault().unregister(this);
        }
        App.getInstance().activityDestroy(this);
        LogUtils.i(TAG,"onDestroy");
    }



    /**
     * 解决软键盘挡住输入框
     */
    protected void setKeyBoardHerlper() {

    }

    /**
     * 沉浸式状态栏
     */
    protected void setStatusBar() {
        ImmersiveUtils.setStatusBar(this, false, true);
        ImmersiveUtils.setStatusTextColor(true, this);
    }

    /**
     * 布局文件
     */
    protected abstract int getLayoutId();

    /**
     * 初始界面
     */
    protected abstract void initView();

    /**
     * 初始数据
     */
    protected abstract void initData();

    //Dialog
    private CopyOnWriteArrayList<DialogInterface> dialogs = new CopyOnWriteArrayList<>();

    public void dialogShow(Dialog dialog) {
        dialog.show();
        dialogs.add(dialog);
    }

    public void dialogCancel(DialogInterface dialog) {
        dialog.cancel();
        dialogs.remove(dialog);
    }

    public void dialogClear() {
        for (DialogInterface dialog : dialogs) {
            dialog.cancel();
            dialogs.remove(dialog);
        }
    }

    /**
     * 获取状态栏高度
     */
    public int getStatusBarHeight() {
        Resources resources = getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    }


    /**
     * 全局的toast
     *
     * @param msg
     */
    public void showToast(String msg) {
        if (mToast == null) {
            mToast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        }
        mToast.setText(msg);
        mToast.show();
    }

    /**
     * 显示加载dialog
     */
    public void showLoadingDialog(String msg) {
        mDialog = new Dialog(this, R.style.customDialog);
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_loading, null);
        TextView tv_loading_text = view.findViewById(R.id.tv_loading_text);
        tv_loading_text.setText(msg);

        mDialog.setContentView(view);
        mDialog.setCancelable(false);
        mDialog.setCanceledOnTouchOutside(false);

        mDialog.show();
    }

    /**
     * 关闭加载dialog
     */
    public void hideLoadingDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        mDialog = null;
    }
}
